package com.zking.dao;

import com.sun.org.apache.xpath.internal.operations.Or;
import com.zking.dao.OrderDao;
import com.zking.entity.Order;
import com.zking.entity.Product;
import com.zking.util.DBUtil;

import java.util.ArrayList;
import java.util.List;

public class OrderDao{
    public static int addOrder(Order order) {
        String sql = "insert into easybuy_order values(null,?,?,?,now(),?,?,?)";
        DBUtil.update(sql,order.getEo_user_id(),order.getEo_user_name(),order.getEo_user_address(),
                order.getEo_cost(),order.getEo_status(),order.getEo_type());
        //得到最新的
        sql = "SELECT * FROM easybuy_order WHERE eo_user_id = ? ORDER BY eo_id DESC;";
        List<Order> list = DBUtil.query(sql, Order.class, order.getEo_user_id());
        if(list.size()>0){
            return list.get(0).getEo_id();
        }
        return -1;
    }

    public static List<Order> getOrderList(int pageIndex, int pageSize) {
        String sql = "SELECT * FROM easybuy_order limit ?,?;";
        return DBUtil.query(sql, Order.class, (pageIndex-1)*pageSize,pageSize);
    }

    public static List<Order> getOrderListAll(){
        return DBUtil.query("SELECT * FROM easybuy_order;", Order.class);
    }

    public static int getMaxPage(int pageSize) {
        List<Order> list = getOrderListAll();
        int total = list.size();
        int maxPage = total/pageSize;
        if(total%pageSize!=0){
            maxPage++;
        }
        return maxPage;
    }

    public static int getMaxPageByIdAndName(int eoId, String eoUserName, int pageSize){
        String sql = "SELECT * FROM easybuy_order ";
        List<Object> params = new ArrayList<>();
        boolean whereAdded = false;

        if (eoId > 0) {
            sql += "WHERE eo_id = ? ";
            params.add(eoId);
            whereAdded = true;
        }
        if (eoUserName != null && !eoUserName.isEmpty()) {
            if (whereAdded) {
                sql += "AND ";
            } else {
                sql += "WHERE ";
            }
            sql += "eo_user_name = ? ";
            params.add(eoUserName);
        }
        int total = DBUtil.query(sql, Order.class, params.toArray()).size();
        int maxPage = total/pageSize;
        if(total%pageSize!=0){
            maxPage++;
        }
        return maxPage;
    }

    public static Order getOrderById(int eoId) {
        return DBUtil.query("SELECT * FROM easybuy_order WHERE eo_id = ?;", Order.class, eoId).get(0);
    }

    public static boolean updateOrder(Order order) {
        String sql = "UPDATE easybuy_order SET eo_user_name = ?,eo_user_address = ?,eo_cost = ?,eo_status = ?,eo_type = ? WHERE eo_id = ?;";
        return DBUtil.update(sql, order.getEo_user_name(),order.getEo_user_address(),order.getEo_cost(),order.getEo_status(),order.getEo_type(),order.getEo_id());
    }

    public static boolean deleteOrder(int eoId) {
        String sql = "delete from easybuy_order where eo_id = ?";
        return DBUtil.update(sql, eoId);
    }

    public static List<Order> getOrderListByIdAndUserName(int eoId, String eoUserName,
                                                          int pageIndex, int pageSize) {
        if(eoId<=0 && (eoUserName==null || eoUserName.isEmpty())){
            return null;
        }

        String sql = "SELECT * FROM easybuy_order ";
        List<Object> params = new ArrayList<>();
        boolean whereAdded = false;

        if (eoId > 0) {
            sql += "WHERE eo_id = ? ";
            params.add(eoId);
            whereAdded = true;
        }
        if (eoUserName != null && !eoUserName.isEmpty()) {
            if (whereAdded) {
                sql += "AND ";
            } else {
                sql += "WHERE ";
            }
            sql += "eo_user_name = ? ";
            params.add(eoUserName);
        }
        sql += "limit ?,?;";
        params.add((pageIndex-1)*pageSize);
        params.add(pageSize);

        System.out.println(sql);
        System.out.println(params);
        return DBUtil.query(sql, Order.class, params.toArray());
    }

    public static boolean alterOrderStatus(int eoId, int eoStatus) {
        String sql = "UPDATE easybuy_order SET eo_status = ? WHERE eo_id = ?;";
        return DBUtil.update(sql, eoStatus, eoId);
    }
}
